package com.water.devtool.ui.define;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;

import com.water.devtool.define.ProjectBean;
import com.water.devtool.ui.UiUtil;
import com.water.util.ConfUtil;
import com.water.util.PathUtil;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class ProjectComposite extends Composite implements TabComposite {
	private ProjectBean project;
	private Text textCode;
	private Text text_desc;
	private Text text_package;
	private Text text_class;
	private Text text_datasource;
	private Text text_seq;
	private Text text_Path;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ProjectComposite(Composite parent, int style) {
		super(parent, style);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setBounds(24, 46, 98, 20);
		lblNewLabel.setText("名称：");

		Button btnNewButton_ok = new Button(this, SWT.NONE);
		btnNewButton_ok.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				project.setDesc(text_desc.getText());
				project.setCode(textCode.getText());
				project.setPackageNameStyle(text_package.getText());
				project.setClassNameStyle(text_class.getText());
				project.setDefaultDataSource(text_datasource.getText());
				project.setDefaultSeqName(text_seq.getText());
				String temp = text_Path.getText();
				if (!temp.endsWith("/")) {
					temp = temp + "/";
				}
				project.setProjectPath(temp);

			}
		});
		btnNewButton_ok.setBounds(335, 403, 98, 30);
		btnNewButton_ok.setText("确定");

		Button btnNewButton_cencel = new Button(this, SWT.NONE);
		btnNewButton_cencel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				closeTab();
			}
		});
		btnNewButton_cencel.setBounds(603, 401, 98, 30);
		btnNewButton_cencel.setText("取消");

		textCode = new Text(this, SWT.BORDER);
		textCode.setBounds(127, 43, 349, 26);

		Label label = new Label(this, SWT.NONE);
		label.setText("描述：");
		label.setAlignment(SWT.RIGHT);
		label.setBounds(43, 238, 76, 20);

		text_desc = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData dataLastName = new GridData();
		dataLastName.grabExcessHorizontalSpace = true;
		dataLastName.horizontalAlignment = GridData.FILL;
		dataLastName.widthHint = 50;
		dataLastName.heightHint = 60;
		text_desc.setLayoutData(dataLastName);
		text_desc.setBounds(124, 238, 352, 117);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("包路径：");
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(24, 98, 98, 20);

		text_package = new Text(this, SWT.BORDER);
		text_package.setBounds(127, 95, 349, 26);

		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("类名称：");
		label_2.setAlignment(SWT.RIGHT);
		label_2.setBounds(494, 98, 98, 20);

		text_class = new Text(this, SWT.BORDER);
		text_class.setBounds(607, 92, 349, 26);

		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("数据源：");
		label_3.setAlignment(SWT.RIGHT);
		label_3.setBounds(24, 152, 98, 20);

		text_datasource = new Text(this, SWT.BORDER);
		text_datasource.setBounds(127, 149, 349, 26);

		Label label_4 = new Label(this, SWT.NONE);
		label_4.setText("序列：");
		label_4.setAlignment(SWT.RIGHT);
		label_4.setBounds(494, 155, 98, 20);

		text_seq = new Text(this, SWT.BORDER);
		text_seq.setBounds(607, 152, 349, 26);

		Label label_5 = new Label(this, SWT.NONE);
		label_5.setText("工程路径：");
		label_5.setAlignment(SWT.RIGHT);
		label_5.setBounds(494, 46, 98, 20);

		text_Path = new Text(this, SWT.BORDER);
		text_Path.setEditable(false);
		text_Path.setBounds(607, 40, 349, 26);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setProject(ProjectBean project) {
		if (this.project != null) {
			return;
		}
		this.project = project;

		UiUtil.setText(textCode, project.getCode());
		UiUtil.setText(text_desc, project.getDesc());
		UiUtil.setText(text_package, project.getPackageNameStyle());
		UiUtil.setText(text_class, project.getClassNameStyle());
		UiUtil.setText(text_datasource, project.getDefaultDataSource());
		UiUtil.setText(text_seq, project.getDefaultSeqName());
		String projectPath = ConfUtil.getString("projectPath", null);
		if (StringUtils.isBlank(projectPath)) {
			projectPath = PathUtil.transcate(ConfUtil.getString("workspace", ""), project.getCode());
		}
		UiUtil.setText(text_Path, projectPath);

	}

	public void closeTab() {
		TabFolder tabFolder = (TabFolder) getParent();
		TabItem[] items = tabFolder.getSelection();
		for (TabItem tabItem : items) {
			tabItem.dispose();
		}
	}
}
